package com.yidu.controller;

import com.yidu.entity.FlType;
import com.yidu.service.FlTypeService;
import com.yidu.utils.BaseMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (FlType)表控制层
 *
 * @author liandyao
 * @since 2021-07-29 16:19:50
 */
@RestController
@RequestMapping("flType")
public class FlTypeController {
    /**
     * 服务对象
     */
    @Resource
    private FlTypeService flTypeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BaseMessage selectOne(Integer id) {
        FlType type = this.flTypeService.queryById(id);
        return BaseMessage.YES("查询成功",type) ;
    }

    @GetMapping("/showList")
    public Map<String,Object> showList(FlType type){
        //查询并分页
        List<FlType> list =  flTypeService.queryAllByLimit(type);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",flTypeService.findCount(type));
        map.put("data",list);
        return map;

    }

    /**
     * 增加和修改
     * @param type
     * @return
     */
    @PostMapping("/addOrUpdate")
    public BaseMessage addOrUpdate(FlType type){
        FlType po = null;
        if(type.getTypeId()==null) {
            po = flTypeService.insert(type);
        }else{
            po=flTypeService.update(type);
        }
        if(po==null){
            return BaseMessage.NO("保存失败");
        }else{
            return BaseMessage.YES("保存成功");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public BaseMessage delete(Integer id){
        boolean flag = flTypeService.deleteById(id);
        return flag?BaseMessage.YES("操作成功"):BaseMessage.NO("操作失败");
    }

    @PostMapping("/deleteBatch")
    public BaseMessage deleteBatch(String ids){
        String[] id = ids.split(",");
        int rows = flTypeService.deleteByIds(id);
        return rows>0?BaseMessage.YES("删除成功"):BaseMessage.NO("删除失败");
    }
}